import io from 'socket.io-client'
import store from '../store'
import { Notification, MessageBox } from 'element-ui'

const socket = io()

socket.on('connect', () => {
  console.log('服务器已经运行了')
})
socket.on('room_info', ({ nicknames, holder, lines }) => {
  store.commit('upDataNickName', nicknames)
  store.commit('upDateHolder', holder)
  store.commit('upDateLines', lines)
})
socket.on('user_enter', name => {
  store.commit('addToName', name)
})
socket.on('connect', () => {
  store.commit('upDateconnection', true)
})
socket.on('disconnect', () => {
  store.commit('upDateconnection', false)
})

socket.on('game_started', holder => {
  store.commit('upDateHolder', holder)
  Notification.success(`${holder} 作为主持人，已经成功开始了游戏，大家可以开始猜答案啦！`)
})
// 监听游戏的终止, 重置信息
socket.on('game_stoped', () => {
  // 1. 清除相关的数据 nickname nicknames holder lines
  store.commit('upDateHolder', '')
  store.commit('upDateLines', [])

  // 2. 弹出提示消息
  Notification.warning('主持人终止了当前的游戏')
})
socket.on('game_answered', ({ alreadyDone, success, nickname, answer }) => {
  console.log(alreadyDone, success, nickname, answer)
  if (!success) {
    Notification.warning('答案错了')
  }
  if (success) {
    MessageBox.alert(nickname + '猜到了答案,答案为' + answer)
  }
  socket.on('already_started', holder => {
    Notification.warning('游戏已经开始了当前主持人是' + holder)
  })
})
//
socket.on('starting_line', line => {
  store.commit('drawNewLine', line)
})
socket.on('updating_line', line => {
  console.log(123)
  store.commit('updateNewLine', line)
})
export default socket
